﻿Imports PTIC_Util
Imports PTIC_GestionBDD
Imports Actuariat
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Collections.Specialized
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Windows.Data
Imports System.Windows.Input
Imports OMANE.Common.UI
Imports OMANE.TraitementsDesigner


Namespace ViewModels

    ''' <summary>
    ''' ViewModel de l'écran de visualisation des traitements
    ''' </summary>
    ''' <remarks></remarks>
    Public Class ViewModelTraitementsViewer
        Inherits ViewModelCommand
        Implements IViewModelTabSection

        Private _model As ModelTauxGarantis

#Region "Constructeur"
        ''' <summary>
        ''' Constructeur
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub New(ByVal Model As ModelTauxGarantis, ByVal DisplayName As String, ByVal Command As ICommand)
            MyBase.New(DisplayName, Command)
            _model = Locator.ModelStatic
        End Sub
        Public Sub New()
            '_model = Locator.ModelStatic
        End Sub
#End Region

#Region "Propriétés"
        Public Property Icone As String Implements Common.UI.IViewModelTabSection.Icone
        Public Property Title As String Implements Common.UI.IViewModelTabSection.Title
        Private ReadOnly Property Model As ModelTauxGarantis
            Get
                Return _model
            End Get
        End Property

        Private _Graph As TraitementsDesigner.Graph
        Public ReadOnly Property Graph As TraitementsDesigner.Graph
            Get
                If _Graph Is Nothing Then
                    createGraph()
                End If
                Return _Graph
            End Get
        End Property
#End Region

#Region "Création du graph"

        Private Sub createGraph()

            'Création du graph
            Dim graph As New TraitementsDesigner.Graph("Traitements Taux Garanti")

            'Remplissage des noeuds
            Dim lNodesIntegres As New Dictionary(Of Traitement, TraitementNode)
            For Each trait As Traitement In _model.ListeTraitements.Values
                Dim Node As TraitementNode = New TraitementNode(trait)
                lNodesIntegres.Add(trait, Node)
                graph.Nodes.Add(Node)
            Next

            'Remplissage des dépendances
            For Each trait As Traitement In _model.ListeTraitements.Values
                If TypeOf trait Is RequeteBDD Then
                    Dim traitReq As RequeteBDD = DirectCast(trait, RequeteBDD)
                    'Récupérer le noeud relatif à la requête
                    Dim NodeCible As TraitementNode = lNodesIntegres(traitReq)
                    For Each traitDependence As Traitement In traitReq.TraitementsSource.Values
                        'Récupération des noeuds relatifs à la dépendance et alimentation du noeud
                        Dim NodeSource As TraitementNode = lNodesIntegres(traitDependence)
                        NodeSource.NodeDependencies.Add(NodeCible)
                    Next
                End If
            Next

            'Tableau des générations
            Dim generations As New List(Of List(Of TraitementNode))

            'Recherche de la première génération
            generations.Add(New List(Of TraitementNode))
            For Each trait As Traitement In _model.ListeTraitements.Values
                If TypeOf (trait) Is tableBDD _
                    Or TypeOf (trait) Is moteurTauxGaranti1.Moteur_Methode1 _
                    Or TypeOf (trait) Is moteurTauxGaranti2.Moteur_Methode2 Then
                    'Or TypeOf (trait) Is Moteur_EcheancierTxGar _
                    generations(0).Add(lNodesIntegres(trait))
                End If
            Next

            'Recherche des générations suivantes
            RechercheGenerationSuivantes(generations)

            'Mise à jour des positions
            For i = 0 To generations.Count - 1
                Dim j As Integer = 20
                For Each traitNode As TraitementNode In generations(i)
                    traitNode.LocationX = i * 250 + 20
                    traitNode.LocationY = j
                    j = j + 30
                Next
            Next



            _Graph = graph

        End Sub

        Private Sub RechercheGenerationSuivantes (ByVal generations As List(Of List(Of TraitementNode)))

            'Récupération de la dernière génération alimentée
            Dim hasDependencies = False
            Dim nbGenAlim As Integer = generations.Count
            generations.Add(New List(Of TraitementNode))
            For Each TraitNode As TraitementNode In generations(nbGenAlim - 1)
                'Travailler à partir des noeuds
                For Each DependencyNode As TraitementNode In TraitNode.NodeDependencies
                    hasDependencies = True
                    generations(nbGenAlim).Add(DependencyNode)
                Next
            Next
            If hasDependencies Then
                RechercheGenerationSuivantes(generations)
            Else
                generations.RemoveAt(nbGenAlim)
            End If

        End Sub
#End Region

    End Class

    Public Class TraitementNode
        Inherits TraitementsDesigner.Node

        Private _Traitement As Traitement
        Private _Rang As Integer
        Private _dependancies As TraitementNode

        Public Sub New(ByVal Traitement As Traitement)
            MyBase.New(Traitement.nom, 0, 0)
            _Traitement = Traitement
        End Sub
        Public Property Traitement As Traitement
            Get
                Return _Traitement
            End Get
            Set(ByVal value As Traitement)
                value = _Traitement
            End Set
        End Property

    End Class



    'Public Class TraitementNode
    '    Private _Traitement As Traitement
    '    Private _Node As TraitementsDesigner.Node
    '    Private _Dependencies As List(Of Traitement)
    '    Public Sub New(ByVal Traitement As Traitement)
    '        _Traitement = Traitement
    '        _Node = New TraitementsDesigner.Node(_Traitement.nom, 0, 0)
    '        'Récupérer l'orde des dependency pour calculer la Location

    '    End Sub
    'End Class

End Namespace